<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 简单商城</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="auth.css">
</head>
<body>
    <header>
        <h1>简单商城</h1>
        <nav>
            <a href="index.html">首页</a>
            <a href="register.html">注册</a>
        </nav>
    </header>

    <main class="auth-container">
        <div class="auth-box">
            <h2>用户登录</h2>
            <form id="loginForm" onsubmit="return handleLogin(event)">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" class="auth-button">登录</button>
            </form>
            <p class="auth-link">
                还没有账号？<a href="register.html">立即注册</a>
            </p>
        </div>
    </main>

    <footer>
        <p>© 2025 简单商城 - 示例项目</p>
    </footer>

    <script>
        function handleLogin(event) {
            event.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // 从localStorage获取用户数据
            const users = JSON.parse(localStorage.getItem('users') || '{}');
            
            // 验证用户
            if (users[username] && users[username].password === password) {
                // 存储登录状态
                localStorage.setItem('currentUser', username);
                alert('登录成功！');
                window.location.href = 'index.html';
            } else {
                alert('用户名或密码错误！');
            }
        }

        // 检查是否已登录
        window.onload = function() {
            const currentUser = localStorage.getItem('currentUser');
            if (currentUser) {
                window.location.href = 'index.html';
            }
        }
    </script>
</body>
</html>